package com.xc.hotelmgr.webapi.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xc.hotelmgr.webapi.common.utils.PageUtils;
import com.xc.hotelmgr.webapi.common.utils.R;
import com.xc.hotelmgr.webapi.modules.dt.entity.DtEntity;
import com.xc.hotelmgr.webapi.modules.dt.entity.StoreEntity;
import com.xc.hotelmgr.webapi.modules.dt.entity.StorePfEntity;
import com.xc.hotelmgr.webapi.modules.dt.service.DtService;
import com.xc.hotelmgr.webapi.modules.dt.service.StorePfService;
import com.xc.hotelmgr.webapi.modules.dt.service.StoreService;
import com.xc.hotelmgr.webapi.modules.sys.entity.SysMqConsumerEntity;
import com.xc.hotelmgr.webapi.modules.sys.entity.SysMqEntity;
import com.xc.hotelmgr.webapi.modules.sys.service.SysMqConsumerService;
import com.xc.hotelmgr.webapi.modules.sys.service.SysMqService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sys/mq")
@RequiredArgsConstructor
public class SysMqController extends AbstractController{

    private final SysMqService sysMqService;

    private final DtService dtService;

    private final StorePfService storePfService;

    private final SysMqConsumerService sysMqConsumerService;

    private final StoreService storeService;

    /**
     * 查询所有店铺的消息队列配置
     * @return
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = sysMqService.queryPage(params);
        return R.ok().put("page", page);
    }

    @GetMapping("/dt")
    public R dtData() {
        String dtId = getUserDtId().toString();
        String inSql = "SELECT id FROM dt_dt WHERE FIND_IN_SET(id, getChild("+ dtId+ "))";
        List<DtEntity> dts =  dtService.getBaseMapper().selectList(new QueryWrapper<DtEntity>().inSql("id",inSql));
        List<StorePfEntity> spfs = storePfService.getBaseMapper().selectList(new QueryWrapper<StorePfEntity>());
        return R.ok().put("dts", dts).put("spfs",spfs);
    }

    /**
     * 添加消息队列配置信息
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody Map<String, Object> params) {
        Long storeId = Long.valueOf((String)params.get("storeId").toString());
        Long dtId = Long.valueOf((String) params.get("dtId").toString());
        String dtName = (String) params.get("dtName");
        StoreEntity storeEntity = storeService.getById(storeId);
        String storeName = storeEntity.getName();
        String appKey = storeEntity.getAppkey();
        Date date = new Date();
        SysMqEntity sysMqEntity = new SysMqEntity();
        sysMqEntity.setAppKey(appKey);
        sysMqEntity.setDate(date);
        sysMqEntity.setDtId(dtId);
        sysMqEntity.setDtName(dtName);
        sysMqEntity.setStoreId(storeId);
        sysMqEntity.setStoreName(storeName);
        sysMqService.save(sysMqEntity);
        return R.ok();
    }

    /**
     * 删除
     * @return
     */
    @PostMapping("/del")
    public R del(@RequestBody Map<String, Object> params) {
        Integer id = (Integer) params.get("id");
        sysMqService.removeById(id);
        return R.ok();
    }

    /**
     * 更新消费者数量
     * @return
     */
    @PostMapping("update")
    public R update(@RequestBody SysMqConsumerEntity sysMqConsumerEntity) {
        Integer dtId = getUserDtId();
        if(dtId!=1) {
            return R.error("请通知管理员设置");
        }

        boolean result = sysMqService.mqUpdate(sysMqConsumerEntity);
        if(result) {
            sysMqConsumerService.updateById(sysMqConsumerEntity);
        }
        else {
            return R.error("消费者数量设置失败");
        }
        return R.ok("消费者数量设置成功");
    }

    /**
     * 查询消费者个数
     * @return
     */
    @GetMapping("select")
    public R select() {
        List<SysMqConsumerEntity> sysMqConsumerEntities = sysMqConsumerService.getAll();
        return R.ok().put("mq", sysMqConsumerEntities.get(0));
    }

    /**
     * 清空消费者
     * @return
     */
    @PostMapping("/stop")
    public R create(@RequestBody Map<String, Object> params) {
        return R.error("暂未开发");
    }

    /**
     * 启动消费者
     * @return
     */
    @PostMapping("/start")
    public R start(@RequestBody Map<String, Object> params) {
        sysMqService.mqAdd(params);
        return R.ok("消费者启动成功");
    }
}
